package com.buba.record.controller;

import com.buba.pojo.other.Drugs;

import com.buba.pojo.record.DrugRecord;
import com.buba.record.service.DrugRecordService;
import com.buba.record.vo.DrugRecordVv;
import com.buba.utils.R;
import com.buba.utils.config.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 药品记录表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-09
 */
@Slf4j
@RestController
@RequestMapping("/record/drug-record")
public class DrugRecordController {
    @Autowired
    private DrugRecordService drugRecordService;

    //药品表单查
    @GetMapping("/getByIddrugs/{id}")
    public R getByIddrugs(@PathVariable("id") Integer id){
        Drugs drugs=drugRecordService.getByIddrugs(id);
        return R.ok().data("drugs",drugs);
    }

    @PostMapping("/adddrugRecord")
    public R adddrugRecord(@RequestBody DrugRecordVv drugRecordVoo){
        log.info("drugRecord=="+ drugRecordVoo);

        //流水号
        drugRecordVoo.setSerialNumber(RandomUtil.getSixBitRandom());
        BigDecimal number = new BigDecimal(drugRecordVoo.getUseNumber());
        log.info("number:::"+number);
        drugRecordVoo.setPrice(drugRecordVoo.getDrugsPrice().multiply(number));
        log.info("drugRecord=11="+ drugRecordVoo);

//        drugRecordVoo.setPrice(drugRecordVoo.getUseNumber() * drugRecordVoo.getDrugsPrice());
        boolean save=drugRecordService.adddrugRecord(drugRecordVoo);
        if(save==true){
            return R.ok().data("ok","添加成功");
        }else {
            return R.ok().data("error","添加失败");
        }
    }



    //缴费
    @GetMapping("/payment/{id}/{cardId}")
    public R payment(@PathVariable Integer id , @PathVariable Integer cardId){

        int i = drugRecordService.payment(id , cardId);
        DrugRecord byId = drugRecordService.getById(id);
        byId.setDeleteFlag("1");
        boolean b = drugRecordService.updateById(byId);
        if (b){
            return R.ok().data("message" , "缴费成功");
        }else {
            return R.error().data("message" , "缴费失败");
        }
    }

    //缴费明细
    @GetMapping("/payDetail/{id}")
    public R payDetail(@PathVariable Integer id){
        DrugRecordVv drugRecordVo = drugRecordService.payDetail(id);
        return R.ok().data("detail" , drugRecordVo);
    }

    //查询就诊人待缴费记录
    @GetMapping("/pantPayRecor/{id}")
    public R pantPayRecor(@PathVariable Integer id){
        List<DrugRecordVv> drugRecordVos = drugRecordService.pantPayRecor(id);
        return R.ok().data("payRecor" , drugRecordVos);
    }


    //查询就诊人充值记录
    @GetMapping("/patientRecordById/{id}/{begin}/{end}")
    public R patientRecordById(@PathVariable Integer id , @PathVariable String begin , @PathVariable String end){
        log.info("接收前端传入的值：：：："+id+begin+end);
        List<DrugRecordVv> recordVos = drugRecordService.patientRecordById(id , begin , end);
        return R.ok().data("record" , recordVos);
    }

    //查询药品信息
    @GetMapping("/drugMesById/{id}")
    public R drugMesById(@PathVariable Integer id){
        log.info("id:::"+id);
        DrugRecordVv drugMes = drugRecordService.drugMesById(id);
        List<DrugRecordVv> recordVos = drugRecordService.drugPatientById(drugMes.getThePatient().getId());
        DrugRecordVv drugRecordVv = drugRecordService.drugMesById(id);
        List<DrugRecordVv> drugRecordVvs = drugRecordService.drugPatientById(drugMes.getId());

        int rental = 0;
        for (int i = 0; i < recordVos.size(); i++) {
            rental += recordVos.get(i).getPrice().intValue();
        }
        return R.ok().data("drugMes" , drugRecordVv).data("recordVos" , drugRecordVvs).data("rental" , rental);
    }

    //根据就诊人查询缴费记录
    @GetMapping("/drugRecordById/{id}/{begin}/{end}")
    public R drugRecordById(@PathVariable Integer id , @PathVariable String begin , @PathVariable String end){
        log.info("id:::"+id);
        log.info("begin:::"+begin);
        log.info("end:::"+end);
        List<DrugRecordVv> recordVos = drugRecordService.drugRecordById(id , begin , end);
        return R.ok().data("record" , recordVos);
    }


}

